package com.raincat.multimediapicker.utils;

import android.content.Context;
import android.widget.Toast;

import com.raincat.multimediapicker.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
 * Created by Administrator on 2018/3/27 0027.
 */

public class VideoController {
    /**
     * 视频选择数量，恒定为1
     */
    public final static int maxCount = 1;
    /**
     * 视频最大尺寸，MB
     */
    public static int maxSize = 1024;

    /**
     * 是否显示拍照
     */
    public static boolean showCameraOption = false;

    public static LinkedHashMap<String, VideoItem> videoItemMap = new LinkedHashMap<>();
    public static ArrayList<String> realPathList = new ArrayList<>();
    public static int count = 0;

    static void initController(PickerConfig pickerConfig) {
        maxSize = pickerConfig.maxSize;
        showCameraOption = pickerConfig.showCameraOption;
    }

    /**
     * 把选中视频放入或移出临时Map
     *
     * @param context   context
     * @param videoItem 视频对象
     * @return 添加返回true，移出返回false
     */
    public static boolean addOrRemoveVideoItem(Context context, VideoItem videoItem) {
        if (videoItemMap.get(videoItem.videoPath) != null) {
            videoItemMap.remove(videoItem.videoPath);
            count--;
            return false;
        } else if (count < maxCount) {
            if (ImageCacheUtils.getFileSize(videoItem.videoPath) / 1024 / 1024 > maxSize) {
                Toast.makeText(context, context.getResources().getString(R.string.picker_select_tips3) + maxSize + "MB", Toast.LENGTH_SHORT).show();
                return false;
            } else {
                videoItemMap.put(videoItem.videoPath, videoItem);
                count++;
                return true;
            }
        }
        Toast.makeText(context, context.getResources().getString(R.string.picker_select_tips1) + maxCount + context.getResources().getString(R.string.picker_select_tips2), Toast.LENGTH_SHORT).show();
        return false;
    }

    /**
     * 根据path移除选中的图片
     *
     * @param context context
     * @param path    图片路径
     * @return 添加返回true，移出返回false
     */
    public static boolean addOrRemoveVideoItem(Context context, String path) {
        VideoItem videoItem = new VideoItem();
        videoItem.videoPath = path;
        videoItem.videoSize = ImageCacheUtils.getFileSize(path);
        return addOrRemoveVideoItem(context, videoItem);
    }

    /**
     * 返回真实图片路径集
     */
    public static void takeRealPathList() {
        realPathList.clear();
        for (VideoItem videoItem : videoItemMap.values()) {
            realPathList.add(videoItem.videoPath);
        }
    }

    static void reset() {
        realPathList.clear();
        videoItemMap.clear();
        count = 0;
    }
}
